<?php
	namespace Qzn\View;
	
	class FriendListView {
		
		private $m_removeButton = "Remove";
		private $m_blockButton = "Block";
		private $m_unblockButton = "Unblock";
		private $m_acceptButton = "Accept";
		private $m_addButton = "Add";
			
		/**
		 * @return string $html
		 */
		public function DoFriendList(\Qzn\Model\FriendListHandler $flh, \Qzn\View\LoginView $lw, \Qzn\Model\ProfileHandler $ph) {
			$flPk = \Qzn\View\NavigationView::GetFriendList();
			$friendList = $flh->GetFriendList($flPk);
			$user = $ph->GetProfileData($flPk);
			
			if (count($friendList) != 0) {
				$html = "<div>
							<h3>Friend List of $user[0]</h3>
							<form method='post'>
								<fieldset>
									<ul>";
				$pending = "<h3>Pending</h3>";
				$blocked = "<h3>Blocked</h3>";
				$friends = "<h3>Friends</h3>";
				
				foreach ($friendList as $value) {					
					$tempPk = $value[2] == $flPk ? $value[1] : $value[2];
					if (!$this->MyFriendList($lw)) {
						if ($value[3] == 1) {
							$html .= "<li><a href='?profile=$tempPk'>$value[0]</a></li>";
						}
					} else if ($value[3] == 1) {
						$friends .= "<li><input type='submit' name='$tempPk' value='$this->m_blockButton' /><input type='submit' name='$tempPk' value='$this->m_removeButton' /><a href='?profile=$tempPk'>$value[0]</a></li>";
					} else if ($value[3] == 2) {
						$blocked .= "<li><input type='submit' name='$tempPk' value='$this->m_unblockButton' /><a href='?profile=$tempPk'>$value[0]</a></li>";
					} else if ($value[3] == 3) {
						if ($value[2] == $flPk) {
							$pending .= "<li><input type='submit' name='$tempPk' value='$this->m_blockButton' /><input type='submit' name='$tempPk' value='$this->m_removeButton' /><input type='submit' name='$tempPk' value='$this->m_acceptButton' /><a href='?profile=$tempPk'>$value[0]</a></li>";
						} else {
							$pending .= "<li><input type='submit' name='$tempPk' value='$this->m_removeButton' /><a href='?profile=$tempPk'>$value[0]</a></li>";
						}
					}
				}
				
				if ($this->MyFriendList($lw)) {
					$html .= $pending;
					$html .= $friends;
					$html .= $blocked;
				}
				
				$html .= "			</ul>
								</fieldset>
							</form>
						</div>";			
				
				return $html;
			} else if (count($user) != 0) {
				return "<p>This user does not have any friends</p>";
			} else {
				return "<p>This user does not exist</p>";
			}
		}
		
		/**
		 * Returns true or false if its my friend list
		 * @param LoginView
		 * @return boolean
		 */
		public function MyFriendList(\Qzn\View\LoginView $lw) {
			return $lw->GetUserPk() == \Qzn\View\NavigationView::GetFriendList();
		}
		
		/**
		 * @return boolean removeButton
		 */
		public function TriedToRemoveFriend() {
			if (count(array_keys($_POST, "$this->m_removeButton")) == 1) {
				return true;
			}
			
			return false;
		}
		
		/**
		 * @return boolean acceptButton
		 */
		public function TriedToAcceptFriend() {
			if (count(array_keys($_POST, "$this->m_acceptButton")) == 1) {
				return true;
			}
			
			return false;
		}
		
		/**
		 * @return boolean unblockButton
		 */
		public function TriedToUnblockFriend() {
			if (count(array_keys($_POST, "$this->m_unblockButton")) == 1) {
				return true;
			}
			
			return false;
		}
		
		/**
		 * @return boolean blockButton
		 */
		public function TriedToBlockFriend() {
			if (count(array_keys($_POST, "$this->m_blockButton")) == 1) {
				return true;
			}
			
			return false;
		}
		
		/**
		 * @return string $ret
		 */
		public function GetRemoveFriend() {
			if (count(array_keys($_POST, "$this->m_removeButton")) == 1) {
				$ret = array_keys($_POST, "$this->m_removeButton");
				$ret = end($ret);
				return $ret;
			}
			return null;
		}
		
		/**
		 * @return string $ret
		 */
		public function GetBlockFriend() {
			if (count(array_keys($_POST, "$this->m_blockButton")) == 1) {
				$ret = array_keys($_POST, "$this->m_blockButton");
				$ret = end($ret);
				return $ret;
			}
			return null;
		}
		
		/**
		 * @return string $ret
		 */
		public function GetAcceptFriend() {
			if (count(array_keys($_POST, "$this->m_acceptButton")) == 1) {
				$ret = array_keys($_POST, "$this->m_acceptButton");
				$ret = end($ret);
				return $ret;
			}
			return null;
		}
		
		/**
		 * @return string $ret
		 */
		public function GetUnblockFriend() {
			if (count(array_keys($_POST, "$this->m_unblockButton")) == 1) {
				$ret = array_keys($_POST, "$this->m_unblockButton");
				$ret = end($ret);
				return $ret;
			}
			return null;
		}
	}
?>